package listener;

import gui.DayPanel;
import gui.Sprache;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;

import about.JDialoge;
import urlaub.Urlaub;
import vars.Variables;

public class MyMenuListener implements ActionListener, Variables {

	private DayPanel dp;
	private JMenu feast;
	private Sprache sprachen;
	private JMenu sprache;

	public MyMenuListener(DayPanel dp, Sprache sprachen) {
		this.dp = dp;
		this.setSprachen(sprachen);
	}

	@Override
	public void actionPerformed(ActionEvent e) {

		int anzahlItems = getFeast().getMenuComponentCount();
		int anzahlsprachen = getSprache().getMenuComponentCount();
		String action = e.getActionCommand();

		if (action.equals("end")) {
			System.exit(0);

		} else if (action.equalsIgnoreCase("about")) {

			new JDialoge();

		} else if (action.equalsIgnoreCase("urlaub")) {

			new Urlaub(this.dp.getCalF());

		} else {
			if (action.substring(0, 2).equals("c_")) {
				String[] currentItem = action.split("_");

				// alle haken weg machen
				for (int i = 0; i < anzahlItems; i++) {
					JCheckBoxMenuItem local = (JCheckBoxMenuItem) getFeast()
							.getMenuComponent(i);
					local.setSelected(false);
				}

				// haken an aktuellem
				((JCheckBoxMenuItem) getFeast().getMenuComponent(
						new Integer(currentItem[1]).intValue()))
						.setSelected(true);

				this.dp.setSelectedCountry(new Integer(currentItem[1])
						.intValue());
				this.dp.setSelectedFerienCountry(new Integer(currentItem[1])
						.intValue());
				this.dp.showFeastForCountry(currentItem[1]);
				this.dp.getCalF().setSelectCountry(
						new Integer(currentItem[1]).intValue());
			} else {

				String[] currentItem = action.split("_");

				for (int i = 0; i < anzahlsprachen; i++) {

					JCheckBoxMenuItem speaker = (JCheckBoxMenuItem) getSprache()
							.getMenuComponent(i);
					speaker.setSelected(false);

				}
				((JCheckBoxMenuItem) getSprache().getMenuComponent(
						new Integer(currentItem[1]).intValue()))
						.setSelected(true);
				this.sprachen
						.setSprache(new Integer(currentItem[1]).intValue());
				this.sprachen.refresh();

			}
		}
	}

	public JMenu getFeast() {
		return feast;
	}

	public void setFeast(JMenu feast) {
		this.feast = feast;
	}

	public void setSprachen(Sprache sprachen) {
		this.sprachen = sprachen;
	}

	public Sprache getSprachen() {
		return sprachen;
	}

	public void setSprache(JMenu sprache) {
		this.sprache = sprache;
	}

	public JMenu getSprache() {
		return sprache;
	}
}
